<?php
include 'db.php';
include 'pricelist_functions.php';

//that's the crap that has to go to 'pricelists' table
$pricelist_name = $_POST['pricelist_name'];
$pricelist_markup = $_POST['pricelist_markup'];
$pricelist_replacement_discount = $_POST['pricelist_replacement_discount'];
$pricelist_round_to = $_POST['pricelist_round_to'];
$pricelist_facility = $_POST['pricelist_facility'];
if( 0 == $pricelist_facility )
{
	$output = 'You must select a facility!';
	echo $output;
	return;
}

//add new pricelist to the pricelist list
$pricelist_add->bind_param( 'sddii', $pricelist_name, $pricelist_markup, $pricelist_replacement_discount, $pricelist_round_to, $pricelist_facility );
$pricelist_add->execute();
if( -1 == $pricelist_add->affected_rows )
{
	echo 'Unable to create pricelist with name "'.$pricelist_name.'" - pricelist already exists!<br/>';
	$pricelist_add->close();
	return;
}
$pricelist_add->close();

//get the ID of the new pricelist
//$pricelist_get_by_name = $db_corp->prepare( "SELECT pricelist_id FROM pricelists WHERE pricelist_name = ?" );
$pricelist_get_by_name->bind_param( 's', $pricelist_name );
$pricelist_get_by_name->bind_result( $pricelist_id );
$pricelist_get_by_name->execute();
$pricelist_get_by_name->fetch();
$pricelist_get_by_name->close();

//we need to extract BPO IDs of all the stuff that needs to be calculated
$bpos_to_add;
foreach( $_POST as $bpo_id => $status )
{
	if( preg_match( '/bpo_(\d+)/', $bpo_id, $matched_ids ) && ($status == 'on'))
	{
		$bpos_to_add[] = $matched_ids[1];
	}
}

if( 0 == $bpos_to_add )
{
	$output = 'You must select at least one BPO!';
	echo $output;
	return;
}

//we need a list of corp base materials so we know when to stop recursing down material list
$corp_base_materials = array();
$show_corp_prices->bind_result( $base_name, $base_typeID, $base_price, $base_volume );
$show_corp_prices->execute();
while( $show_corp_prices->fetch() )
{
	$corp_base_materials[ $base_typeID ] = array('price' => $base_price, 'volume' => $base_volume);
}
$show_corp_prices->close();

//we will need facility data
$get_facility_by_id->bind_param( 'i', $pricelist_facility );
$get_facility_by_id->bind_result( $fac_name, $fac_mm, $fac_tm, $fac_ic, $fac_phc, $fac_type );
$get_facility_by_id->execute();
$get_facility_by_id->fetch();
$get_facility_by_id->close();

$pricelist = array();
//now we should get all the materials required for a build job
foreach( $bpos_to_add as $bpo_id )
{
	$bpo = get_bpo_production_cost( $bpo_id, $fac_mm, $fac_tm, $fac_ic, $fac_phc, $corp_base_materials );

	//echo 'FINAL BPO: '.$bpo[ 'typeID' ].'->'.$bpo[ 'product_typeID' ].', cost: '.$bpo[ 'build_cost' ].', volume: $bpo[ 'materials_volume' ].'<br />';

	$item_price = ceiling( $bpo[ 'build_cost' ] * (1 + $pricelist_markup/100), $pricelist_round_to );
	$item_replacement_price = ceiling( $item_price * ( 1 - $pricelist_replacement_discount / 100), $pricelist_round_to );

	$pricelist_insert_data->bind_param( 'iiidddd', $pricelist_id, $bpo[ 'typeID' ], $bpo[ 'product_typeID' ], $bpo[ 'build_cost' ], $item_price, $item_replacement_price, $bpo[ 'materials_volume' ] );
	$pricelist_insert_data->execute();

	if( -1 == $pricelist_insert_data->affected_rows )
	{
		echo "Unable to insert row!<br/>!";
	}
}
$pricelist_insert_data->close();

include 'view_pricelists.php';

?>

